<template>
    <view id="certitfy">
        <view>
            <view @tap="chooseAddress" >
                <van-field
                        :value="address_info"
                        placeholder="请选择收货地址"
                        :label="label"
                        input-align="left"
                        disabled
                        :border="has_boder"
                />
            </view>
            <view>
                <van-field
                        :value="number"
                        placeholder=""
                        label="提货数量："
                        input-align="left"
                        disabled
                        :border="has_boder"
                />
            </view>
            <view>
                <van-field
                        :value="remark"
                        placeholder="请输入备注(选填)"
                        :label="label"
                        input-align="left"
                        :border="has_boder"
                        @input="confirm1"
                />
            </view>

        </view>


        <view class="button-submit" @tap="commit()">
            <van-button type="info" round size="large" color="#FF9900" >确定</van-button>
        </view>
    </view>
</template>

<script>
    import MInput from '@/components/m-input.vue'
    export default{
        name:"profitPositionPickUp",
        data(){
            return {
                profit_position_id:'',
                has_boder: true,
                number:'0',
                remark:'',
                address_info:'',
                address_id:'',
                label:'',
                is_business: 0,
                choose_address_id:''
            }

        },
        onLoad(query){
            let market_value=query.market_value
            if(market_value>=1){
                this.number=1
            }else{
                this.number=0
            }
            if(query.is_business!==undefined && query.is_business>0){
                this.is_business=1
            }
            this.profit_position_id=query.profit_position_id
        },
        onShow(){
            this.choose_address_id=this.$store.state.chooseAddressId
            this.getAddressInfo()
        },
        mounted(){
        },
        methods:{
            chooseAddress(){
                this.$router.push({
                route:this.$routesConfig.addressList,
              })
            },


           async commit(){ //确认提交
                if(this.address_id==='' || this.address_info===''){
                    uni.showToast({
                        title:"请设置收货地址",
                        icon:"none",
                    })
                }
                if(this.number<1){
                    uni.showToast({
                        title:"数量不足，不能提货",
                        icon:"none",
                    })
                }
               uni.showLoading({})
               if(this.is_business>0){  // 持仓提货
                   let res =await  this.$apis.positionPickUp({address_id:this.address_id,position_id:this.profit_position_id,remark:this.remark})
                   if(res.code===0){
                       uni.showToast({
                           title:"提货成功",
                           icon:"success",
                       })
                       setTimeout(()=>{
                           uni.reLaunch({
                               url: '/pages/main/business'
                           })
                       },1000)
                   }
               }else{  // 销售配货提货
                   let res =await  this.$apis.myProfitPositionPickUp({address_id:this.address_id,profit_position_id:this.profit_position_id,remark:this.remark})
                   if(res.code===0){
                       uni.showToast({
                           title:"提货成功",
                           icon:"success",
                       })
                       setTimeout(()=>{
                           this.$router.push({
                               route:this.$routesConfig.myWallet,
                           })
                       },1000)
                   }
               }
               uni.hideLoading()
            },

            confirm1(event){
                this.remark=event.detail
            },
            async getAddressInfo(){ // 获取默认地址,如果没有默认地址则去取第一个地址
                if(this.choose_address_id!==''){
                    let resData = await this.$apis.getAddressInfo({id:this.choose_address_id})
                    this.address_info=resData.data.address.province+resData.data.address.city+resData.data.address.district+resData.data.address.house_number
                    this.address_id=resData.data.address.id
                }else{
                    let resData = await this.$apis.getDefaultAddress({access_token:''})
                    if(resData.data.address.id!==undefined){
                        this.address_info=resData.data.address.province+resData.data.address.city+resData.data.address.district+resData.data.address.house_number
                        this.address_id=resData.data.address.id
                    }else{
                        let otherAddress = await  this.$apis.getAddressList({access_token:''})
                        if(otherAddress.data.addressList.length>0){
                            this.address_info=otherAddress.data.addressList[0].province+otherAddress.data.addressList[0].city+otherAddress.data.addressList[0].district+otherAddress.data.addressList[0].house_number
                            this.address_id=otherAddress.data.addressList[0].id
                        }
                    }
                }

            },
        },
        components:{
            MInput
        }
    }
</script>

<style lang="less" scoped>
    .button-submit{
        width: 90%;
        margin-left: 5%;
        margin-top: 10rpx;
    }
    .tips{
        position: fixed;
        width: 100%;
        bottom: 20rpx;
        font-size: 25rpx;
        text-align: center;
        color: red;
    }
</style>
